package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    // 分页查询供应商信息
    @PostMapping("/list")
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        return customerService.list(page, rows, customerName);
    }
    //添加和修改客户
    @PostMapping("/save")//http://localhost:8080/ customer/save?customerId=1
    public ServiceVO saveOrUpdate(Customer customer,
                                  HttpServletRequest request) {
        String customerId = request.getParameter("customerId");
        this.customerService.saveOrUpdate(customer, customerId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @PostMapping("/delete")//http://localhost:8080/customer/delete
    public ServiceVO delete(String ids) throws Exception {
        String[] idsStr = ids.split(",");
        for (int i = 0; i < idsStr.length; i++) {
            int id = Integer.parseInt(idsStr[i]);
            customerService.delete(id);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
    @PostMapping("/getComboboxList")//http://localhost:8080/customer/getComboboxList
    public List<Customer> getComboboxList(String q){
        return customerService.getComboboxList(q);
    }
}
